package com.zzw.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zzw.entity.User;
import com.zzw.service.UserService;
import com.zzw.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/hello")
    public JsonResult hello() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData("hello");
        return jsonResult;
    }

    @RequestMapping("/add")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试插入
     * @Date:2017/10/30 17:21
     * @Param:[]
     * @return com.zzw.web.JsonResult
     */
    public JsonResult add() {
        JsonResult jsonResult = new JsonResult();
        User u = new User();
        int i = (int) (Math.random() * 100);
        u.setId(i);
        u.setUsername("test" + i);
        u.setPassword("passs" + i);
        u = this.userService.insertUser(u);
        jsonResult.setData(u);
        return jsonResult;
    }

    @RequestMapping("/get/{id}")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试根据id查询
     * @Date:2017/10/30 17:23
     * @Param:[id]
     * @return com.zzw.web.JsonResult
     */
    public JsonResult findById(@PathVariable("id") int id) {
        JsonResult jsonResult = new JsonResult();
        User u = new User();
        u.setId(id);
        u = userService.selectOne(u);
        String ret = (u == null ? "找不到对象" : (u.getId() + "    " + u.getUsername()));
        jsonResult.setData(ret);
        return jsonResult;
    }

    @RequestMapping("/update")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试修改
     * @Date:2017/10/30 17:25
     * @Param:[id, userName]
     * @return com.zzw.web.JsonResult
     */
    public JsonResult update(int id, String userName, String password) {
        JsonResult jsonResult = new JsonResult();
        User u = new User();
        u.setId(id);
        u.setUsername(userName);
        u = userService.selectOne(u);
        if (u != null) {
            u.setUsername(userName);
        } else {
            return jsonResult;
        }
        u = userService.updateUser(u);
        jsonResult.setData(u);
        return jsonResult;
    }

    @RequestMapping("/delete/{id}")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试删除
     * @Date:2017/10/30 17:27
     * @Param:[id]
     * @return com.zzw.web.JsonResult
     */
    public JsonResult delete(@PathVariable("id") int id) {
        JsonResult jsonResult = new JsonResult();
        User user = new User();
        user.setId(id);
        int num = userService.deleteById(user);
        jsonResult.setData(num);
        return jsonResult;
    }

    @RequestMapping(value = "/{uid}")
    public JsonResult testUserDao(@PathVariable("uid") int uid) {
        JsonResult jsonResult = new JsonResult();
        User user = new User();
        user.setId(uid);
        User user1 = userService.selectOne(user);
        jsonResult.setData(user1);
        return jsonResult;
    }

    @RequestMapping("/deleteAll")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试删除全部
     * @Date:2017/10/30 17:28
     * @Param:[]
     * @return java.lang.String
     */
    public JsonResult deleteAll() {
        JsonResult jsonResult = new JsonResult();
        int num = userService.deleteAll();
        jsonResult.setData(num);
        return jsonResult;
    }

    @RequestMapping("/queryPage")
    /**
     * @Athor:xiezhongjiang
     * @Description:测试分页插件
     * @Date:2017/10/30 17:30
     * @Param:[]
     * @return com.zzw.web.JsonResult
     */
    public JsonResult queryPage() {
        JsonResult jsonResult = new JsonResult();
        PageInfo<User> page = this.userService.queryPage("tes", 1, 5);
        jsonResult.setData(page);
        return jsonResult;
    }

    @RequestMapping("/condition")
    public JsonResult findUserByCondition() {
        JsonResult jsonResult = new JsonResult();
        List<User> users = userService.findUserByCondition();
        jsonResult.setData(users);
        return jsonResult;
    }

    @RequestMapping("/testTransational")
    /**
     * @return
     * @Athor:xiezhongjiang
     * @Description:测试事务
     * @Date:2017/11/1 11:13
     * @Param:
     */
    public JsonResult testTransational() {
        JsonResult jsonResult = new JsonResult();
        try {
            this.userService.batchDealWithUsers();
            jsonResult.setData("success");
        } catch (Exception e) {
            jsonResult.setData(e.getMessage());
        }
        return jsonResult;
    }
}
